package com.zhuozhou.jdyc.main.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseFragment;
import com.zhuozhou.jdyc.databinding.MeFragmentBinding;
import com.zhuozhou.jdyc.main.main.activity.MainActivity;
import com.zhuozhou.jdyc.main.main.viewmodel.MainViewModel;
import com.zhuozhou.jdyc.main.meinfo.activity.BindingPhoneActivity;
import com.zhuozhou.jdyc.main.meinfo.activity.ConcernMerchantActivity;
import com.zhuozhou.jdyc.main.meinfo.activity.DistributionActivity;
import com.zhuozhou.jdyc.main.meinfo.activity.MyCouponActivity;
import com.zhuozhou.jdyc.main.meinfo.activity.OrderActivity;
import com.zhuozhou.jdyc.main.meinfo.activity.RegisteredAgentActivity;
import com.zhuozhou.jdyc.main.meinfo.activity.ShippingAddressActivity;
import com.zhuozhou.jdyc.main.meinfo.activity.SpoorAndCollectActivity;
import com.zhuozhou.jdyc.main.meinfo.activity.WalletActivity;
import com.zhuozhou.jdyc.main.user.activity.LoginActivity;
import com.zhuozhou.jdyc.main.user.bean.UserInfo;
import com.zhuozhou.jdyc.util.media.MediaBuilder;
import com.zhuozhou.jdyc.util.media.MediaHelper;
import com.zhuozhou.jdyc.util.media.MediaTypeEnum;
import com.zhuozhou.jdyc.wight.dialog.CallDialog;
import com.zhuozhou.jdyc.wight.dialog.OpenImageDialog;

/**
 * Created by 方舟 on 2020/03/26.
 * describe：我的页面
 */
public class MeFragment extends BaseFragment<MainViewModel, MeFragmentBinding> {
    private MediaHelper mediaHelper;

    @Override
    protected int getLayoutId() {
        return R.layout.me_fragment;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        if (getActivity() != null && !getActivity().isFinishing()) {
            ((MainActivity) getActivity()).setThemeBarAndToolBarColor(R.color.themeColor);
        }
        mediaHelper = new MediaBuilder(this, this)
                .setImageAlbumCount(1)
                .builder();
        mediaHelper.getMutableLiveData().observe(this, mediaBean -> {
            if (mediaBean.getMediaType() == MediaTypeEnum.IMAGE.getMediaType()) {
                if (mediaBean.getMediaList() == null || mediaBean.getMediaList().size() == 0) {
                    return;
                }
                UserInfo userInfo = UserAccountHelper.getUser();
                userInfo.getNav().get(0).setAvatar(mediaBean.getMediaList().get(0));
                UserAccountHelper.saveLoginState(userInfo, true);
                binding.setUser(UserAccountHelper.getUser());
            }
        });
        binding.tvUserName.setOnClickListener(v -> LoginActivity.show(this, new Bundle()));
//        binding.headImg.setOnClickListener(v -> {
//            if (!UserAccountHelper.isLogin()) {
//                LoginActivity.show(v.getContext(), new Bundle());
//            } else {
//                new OpenImageDialog(getActivity())
//                        .setMediaType(OpenImageDialog.CAMERA_ALBUM)
//                        .setOnOpenImageClickListener(mediaHelper)
//                        .builder()
//                        .show();
//            }
//        });
        mViewModel.BasicsStatisticsMutableLiveData.observe(this, v -> {
            binding.setInfo(v);
            binding.tvService.setOnClickListener(j -> {
                new CallDialog(getActivity(), v.getObj().getPhone()).call();
            });
        });
        binding.tvSeeAll.setOnClickListener(v -> {
            openOrder(0);
        });
        binding.llPayment.setOnClickListener(v -> {
            openOrder(1);
        });
        binding.llDeliver.setOnClickListener(v -> {
            openOrder(2);
        });
        binding.llReceiving.setOnClickListener(v -> {
            openOrder(3);
        });
        binding.llEvaluate.setOnClickListener(v -> {
            openOrder(4);
        });
        binding.tvWallet.setOnClickListener(v -> {
            Intent in = new Intent(getActivity(), WalletActivity.class);
            Bundle bundle = new Bundle();
            in.putExtras(bundle);
            startActivity(in);
        });
        binding.tvWithdrawal.setOnClickListener(v -> {
            Intent in = new Intent(getActivity(), WalletActivity.class);
            Bundle bundle = new Bundle();
            in.putExtras(bundle);
            startActivity(in);
        });

        binding.llCollecting.setOnClickListener(v -> {
            Intent in = new Intent(getActivity(), SpoorAndCollectActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("type", 1);
            in.putExtras(bundle);
            startActivity(in);
        });
        binding.llFootprint.setOnClickListener(v -> {
            Intent in = new Intent(getActivity(), SpoorAndCollectActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("type", 2);
            in.putExtras(bundle);
            startActivity(in);
        });
        binding.llConcern.setOnClickListener(v -> {
            Intent in = new Intent(getActivity(), ConcernMerchantActivity.class);
            startActivity(in);
        });
        binding.tvCoupon.setOnClickListener(v -> {
            Intent in = new Intent(getActivity(), MyCouponActivity.class);
            startActivity(in);
        });
        binding.tvDistribution.setOnClickListener(v -> {
            Intent in = new Intent(getActivity(), DistributionActivity.class);
            startActivity(in);
        });
        binding.tvMobile.setOnClickListener(v -> {
            Intent in = new Intent(getActivity(), BindingPhoneActivity.class);
            startActivity(in);
        });
        binding.tvAgency.setOnClickListener(v -> {
            Intent in = new Intent(getActivity(), RegisteredAgentActivity.class);
            startActivity(in);
        });
        binding.tvLocation.setOnClickListener(v -> {
            Intent in = new Intent(getActivity(), ShippingAddressActivity.class);
            startActivity(in);
        });
    }

    public void openOrder(int type) {
        Intent in = new Intent(getActivity(), OrderActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        in.putExtras(bundle);
        startActivity(in);
    }

    @Override
    public void onResume() {
        super.onResume();
        binding.setUser(UserAccountHelper.getUser());
        mViewModel.getBasicsStatistics(UserAccountHelper.getUserID() + "");
    }

    @Override
    protected void initData(Bundle bundle) {
        mViewModel.getBasicsStatistics(UserAccountHelper.getUserID() + "");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mediaHelper.onActivityResult(requestCode, resultCode, data);
    }
}
